package com.betterjr.modules.loan.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.betterjr.common.service.BaseService;
import com.betterjr.common.utils.BTAssert;
import com.betterjr.mapper.pagehelper.Page;
import com.betterjr.modules.loan.dao.ScfPayRecordMapper;
import com.betterjr.modules.loan.entity.ScfPayRecord;

@Service
public class ScfPayRecordService extends BaseService<ScfPayRecordMapper, ScfPayRecord> {

    /**
     * 新增还款记录
     * @param anRecord
     * @return
     */
    public int addPayRecord(ScfPayRecord anRecord) {
        BTAssert.notNull(anRecord, "新增还款记录失败-anRecord不能为空");
        anRecord.init(anRecord);
        return this.insert(anRecord);
    }

    /**
     * 修改还款记录
     * 
     * @param anRecord
     * @return
     */
    public int saveModifyPayRecord(ScfPayRecord anRecord, Long anId) {
        BTAssert.notNull(anRecord, "修改还款记录失败-anRecord不能为空");

        final Map<String, Object> map = new HashMap<String, Object>();
        map.put("custNo", anRecord.getCustNo());
        map.put("id", anId);
        BTAssert.notNull(selectByClassProperty(ScfPayRecord.class, map), "修改还款记录失败-原始数据不存在");

        anRecord.initModify();
        anRecord.setId(anId);
        return this.updateByPrimaryKeySelective(anRecord);
    }

    /**
     * 查询还款计划列表
     * 
     * @param anMap
     * @param anFlag
     * @param anPageNum
     * @param anPageSize
     * @return
     */
    public Page<ScfPayRecord> queryPayRecordList(Map<String, Object> anMap, int anFlag, int anPageNum, int anPageSize) {
        return this.selectPropertyByPage(anMap, anPageNum, anPageSize, 1 == anFlag);
    }

    /**
     * 查询还款计划列表
     * 
     * @param anMap
     * @param anFlag
     * @param anPageNum
     * @param anPageSize
     * @return
     */
    public List<ScfPayRecord> findPayRecordList(Map<String, Object> anMap) {
        return this.selectByClassProperty(ScfPayRecord.class, anMap);
    }

    /**
     * 查询还款记录详情
     * 
     * @param anId
     * @return
     */
    public ScfPayRecord findPayRecordDetail(Long anId) {
        BTAssert.notNull(anId, "查询还款记录详情失败-anId不能为空");
        return this.selectByPrimaryKey(anId);
    }

    /**
     * 
     * @Title: queryStatisticBeforeRepaymentAndLoad 
     * @Description: TODO(查询资金方过去十二个月的回款及放款情况) 
     * @param @param anMap
     * @param @return 参数说明 
     * @return Map<String,Object> 返回类型 
     * @throws 
     * @author chenlq
     * @date 2017年11月23日 上午11:09:27
     */
    public Map<String, Object> queryStatisticBeforeRepaymentAndLoad(Map<String, Object> anMap) {
        BTAssert.notNull(anMap, "查询条件不能为空！");
        // 记录当前日期，包括本月后的十二个月
        String currentDate = anMap.get("currentDate").toString();
        final String factorNo = anMap.get("factorNo").toString();
        String currentMonthLastDay = getLastDayOfMonth(currentDate);
        int monthValue = 0;
        // 统计回款
        final Map<String, Object> statisticReturnMap = new HashMap<>();
        // 统计放款
        final Map<String, Object> statisticLoadMap = new HashMap<>();
        // 记录回款和放款
        final Map<String, Object> statisticPayAndLoadMap = new HashMap<>();
        // 记录过去每月放款的总金额
        BigDecimal firstMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal twoMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal threeMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal fourMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal fiveMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal sixMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal sevenMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal eightMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal nineMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal tenMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal elevenMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal twelveMonthLoadTotalBalance = new BigDecimal(0);

        // 记录过去每月回款的总金额
        BigDecimal firstMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal twoMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal threeMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal fourMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal fiveMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal sixMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal sevenMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal eightMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal nineMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal tenMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal elevenMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal twelveMonthReturnTotalBalance = new BigDecimal(0);
        for (int i = 0; i < 12; i++) {
            if (i == 0) {
                // 记录放款
                final BigDecimal loadBalance = StatisticEveryMonthLoadBalance(factorNo,
                        currentDate.substring(0, 6) + "01", currentDate);
                // 统计所有的放款金额
                firstMonthLoadTotalBalance = firstMonthLoadTotalBalance.add(loadBalance);
                // 记录回款
                final BigDecimal returnBalance = StatisticEveryMonthReturnBalance(factorNo,
                        currentDate.substring(0, 6) + "01", currentDate);
                // 统计所有的回款款金额
                firstMonthReturnTotalBalance = firstMonthReturnTotalBalance.add(returnBalance);
                statisticLoadMap.put("12", firstMonthLoadTotalBalance);
                statisticReturnMap.put("12", firstMonthReturnTotalBalance);
            }
            if (i != 0) {
                currentDate = currentDate.substring(0, 4) + (Integer.parseInt(currentDate.substring(4, 6)) - 1) + "01";
                monthValue = Integer.parseInt(currentDate.substring(4, 6));
                if (monthValue < 1) {
                    currentDate = Integer.parseInt(currentDate.substring(0, 4)) - 1 + "1201";
                }
                if (currentDate.length() == 7) {
                    currentDate = currentDate.substring(0, 4) + "0" + currentDate.substring(4, 7);
                }
                currentMonthLastDay = getLastDayOfMonth(currentDate);

                if (i == 1) {
                    // 记录贷款
                    final BigDecimal loadBalance = StatisticEveryMonthLoadBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    twoMonthLoadTotalBalance = twoMonthLoadTotalBalance.add(loadBalance);
                    // 记录回款
                    final BigDecimal returnBalance = StatisticEveryMonthReturnBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    twoMonthReturnTotalBalance = twoMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 2) {
                    // 记录贷款
                    final BigDecimal loadBalance = StatisticEveryMonthLoadBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    threeMonthLoadTotalBalance = threeMonthLoadTotalBalance.add(loadBalance);
                    // 记录回款
                    final BigDecimal returnBalance = StatisticEveryMonthReturnBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    threeMonthReturnTotalBalance = threeMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 3) {
                    // 记录贷款
                    final BigDecimal loadBalance = StatisticEveryMonthLoadBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    fourMonthLoadTotalBalance = fourMonthLoadTotalBalance.add(loadBalance);
                    // 记录回款
                    final BigDecimal returnBalance = StatisticEveryMonthReturnBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    fourMonthReturnTotalBalance = fourMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 4) {
                    // 记录贷款
                    final BigDecimal loadBalance = StatisticEveryMonthLoadBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    fiveMonthLoadTotalBalance = fiveMonthLoadTotalBalance.add(loadBalance);
                    // 记录回款
                    final BigDecimal returnBalance = StatisticEveryMonthReturnBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    fiveMonthReturnTotalBalance = fiveMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 5) {
                    // 记录贷款
                    final BigDecimal loadBalance = StatisticEveryMonthLoadBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    sixMonthLoadTotalBalance = sixMonthLoadTotalBalance.add(loadBalance);
                    // 记录回款
                    final BigDecimal returnBalance = StatisticEveryMonthReturnBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    sixMonthReturnTotalBalance = sixMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 6) {
                    // 记录贷款
                    final BigDecimal loadBalance = StatisticEveryMonthLoadBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    sevenMonthLoadTotalBalance = sevenMonthLoadTotalBalance.add(loadBalance);
                    // 记录回款
                    final BigDecimal returnBalance = StatisticEveryMonthReturnBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    sevenMonthReturnTotalBalance = sevenMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 7) {
                    // 记录贷款
                    final BigDecimal loadBalance = StatisticEveryMonthLoadBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    eightMonthLoadTotalBalance = eightMonthLoadTotalBalance.add(loadBalance);
                    // 记录回款
                    final BigDecimal returnBalance = StatisticEveryMonthReturnBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    eightMonthReturnTotalBalance = eightMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 8) {
                    // 记录贷款
                    final BigDecimal loadBalance = StatisticEveryMonthLoadBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    nineMonthLoadTotalBalance = nineMonthLoadTotalBalance.add(loadBalance);
                    // 记录回款
                    final BigDecimal returnBalance = StatisticEveryMonthReturnBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    nineMonthReturnTotalBalance = nineMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 9) {
                    // 记录贷款
                    final BigDecimal loadBalance = StatisticEveryMonthLoadBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    tenMonthLoadTotalBalance = tenMonthLoadTotalBalance.add(loadBalance);
                    // 记录回款
                    final BigDecimal returnBalance = StatisticEveryMonthReturnBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    tenMonthReturnTotalBalance = tenMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 10) {
                    // 记录贷款
                    final BigDecimal loadBalance = StatisticEveryMonthLoadBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    elevenMonthLoadTotalBalance = elevenMonthLoadTotalBalance.add(loadBalance);
                    // 记录回款
                    final BigDecimal returnBalance = StatisticEveryMonthReturnBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    elevenMonthReturnTotalBalance = elevenMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 11) {
                    // 记录贷款
                    final BigDecimal loadBalance = StatisticEveryMonthLoadBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    twelveMonthLoadTotalBalance = twelveMonthLoadTotalBalance.add(loadBalance);
                    // 记录回款
                    final BigDecimal returnBalance = StatisticEveryMonthReturnBalance(factorNo, currentDate,
                            currentMonthLastDay);
                    twelveMonthReturnTotalBalance = twelveMonthReturnTotalBalance.add(returnBalance);
                }
            }
        }
        // 记录放款的十二个月信息
        statisticLoadMap.put("11", twoMonthLoadTotalBalance);
        statisticLoadMap.put("10", threeMonthLoadTotalBalance);
        statisticLoadMap.put("9", fourMonthLoadTotalBalance);
        statisticLoadMap.put("8", fiveMonthLoadTotalBalance);
        statisticLoadMap.put("7", sixMonthLoadTotalBalance);
        statisticLoadMap.put("6", sevenMonthLoadTotalBalance);
        statisticLoadMap.put("5", eightMonthLoadTotalBalance);
        statisticLoadMap.put("4", nineMonthLoadTotalBalance);
        statisticLoadMap.put("3", tenMonthLoadTotalBalance);
        statisticLoadMap.put("2", elevenMonthLoadTotalBalance);
        statisticLoadMap.put("1", twelveMonthLoadTotalBalance);

        // 记录十二个月回款信息
        statisticReturnMap.put("11", twoMonthReturnTotalBalance);
        statisticReturnMap.put("10", threeMonthReturnTotalBalance);
        statisticReturnMap.put("9", fourMonthReturnTotalBalance);
        statisticReturnMap.put("8", fiveMonthReturnTotalBalance);
        statisticReturnMap.put("7", sixMonthReturnTotalBalance);
        statisticReturnMap.put("6", sevenMonthReturnTotalBalance);
        statisticReturnMap.put("5", eightMonthReturnTotalBalance);
        statisticReturnMap.put("4", nineMonthReturnTotalBalance);
        statisticReturnMap.put("3", tenMonthReturnTotalBalance);
        statisticReturnMap.put("2", elevenMonthReturnTotalBalance);
        statisticReturnMap.put("1", twelveMonthReturnTotalBalance);
        // 记录放款和回款
        statisticPayAndLoadMap.put("payStatistic", statisticReturnMap);
        statisticPayAndLoadMap.put("loadStatistic", statisticLoadMap);
        return statisticPayAndLoadMap;

    }

    /**
     * 
     * @Title: queryStatisticCoreEnterpristBeforeRepaymentAndLoad 
     * @Description: TODO(统计出核心企业过去十二个月的应付款和已付款金额) 
     * @param @param anMap
     * @param @return 参数说明 
     * @return Map<String,Object> 返回类型 
     * @throws 
     * @author chenlq
     * @date 2017年11月29日 下午5:23:42
     */
    public Map<String, Object> queryStatisticCoreEnterpristBeforeRepaymentAndLoad(Map<String, Object> anMap) {
        BTAssert.notNull(anMap, "查询条件不能为空！");
        // 记录当前日期，包括本月后的十二个月
        String currentDate = anMap.get("currentDate").toString();
        final String coreCustNo = anMap.get("coreCustNo").toString();
        String currentMonthLastDay = getLastDayOfMonth(currentDate);
        int monthValue = 0;
        // 统计应付款
        final Map<String, Object> statisticReturnMap = new HashMap<>();
        // 统计已还款
        final Map<String, Object> statisticLoadMap = new HashMap<>();
        // 记录应付款和已还款
        final Map<String, Object> statisticPayAndLoadMap = new HashMap<>();
        // 记录过去每月应付款的总金额
        BigDecimal firstMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal twoMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal threeMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal fourMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal fiveMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal sixMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal sevenMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal eightMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal nineMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal tenMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal elevenMonthLoadTotalBalance = new BigDecimal(0);
        BigDecimal twelveMonthLoadTotalBalance = new BigDecimal(0);

        // 记录过去每月已付款的总金额
        BigDecimal firstMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal twoMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal threeMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal fourMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal fiveMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal sixMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal sevenMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal eightMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal nineMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal tenMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal elevenMonthReturnTotalBalance = new BigDecimal(0);
        BigDecimal twelveMonthReturnTotalBalance = new BigDecimal(0);
        for (int i = 0; i < 12; i++) {
            if (i == 0) {
                // 记录应付款金额
                final BigDecimal loadBalance = StatisticCoreEnterpriseEveryMonthShouldPayBalance(coreCustNo,
                        currentDate.substring(0, 6) + "01", currentDate);
                // 统计当月所有的应付款金额金额
                firstMonthLoadTotalBalance = firstMonthLoadTotalBalance.add(loadBalance);
                // 记录已付款
                final BigDecimal returnBalance = StatisticCoreEnterpriseEveryMonthPayBalance(coreCustNo,
                        currentDate.substring(0, 6) + "01", currentDate);
                // 统计所有的已付款金额
                firstMonthReturnTotalBalance = firstMonthReturnTotalBalance.add(returnBalance);
                statisticLoadMap.put("12", firstMonthLoadTotalBalance);
                statisticReturnMap.put("12", firstMonthReturnTotalBalance);
            }
            if (i != 0) {
                currentDate = currentDate.substring(0, 4) + (Integer.parseInt(currentDate.substring(4, 6)) - 1) + "01";
                monthValue = Integer.parseInt(currentDate.substring(4, 6));
                if (monthValue < 1) {
                    currentDate = Integer.parseInt(currentDate.substring(0, 4)) - 1 + "1201";
                }
                if (currentDate.length() == 7) {
                    currentDate = currentDate.substring(0, 4) + "0" + currentDate.substring(4, 7);
                }
                currentMonthLastDay = getLastDayOfMonth(currentDate);
                if (i == 1) {
                    // 记录应付款
                    final BigDecimal loadBalance = StatisticCoreEnterpriseEveryMonthShouldPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    twoMonthLoadTotalBalance = twoMonthLoadTotalBalance.add(loadBalance);
                    // 记录已付款
                    final BigDecimal returnBalance = StatisticCoreEnterpriseEveryMonthPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    twoMonthReturnTotalBalance = twoMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 2) {
                    // 记录应付款
                    final BigDecimal loadBalance = StatisticCoreEnterpriseEveryMonthShouldPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    threeMonthLoadTotalBalance = threeMonthLoadTotalBalance.add(loadBalance);
                    // 记录已付款
                    final BigDecimal returnBalance = StatisticCoreEnterpriseEveryMonthPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    threeMonthReturnTotalBalance = threeMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 3) {
                    // 记录应付款
                    final BigDecimal loadBalance = StatisticCoreEnterpriseEveryMonthShouldPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    fourMonthLoadTotalBalance = fourMonthLoadTotalBalance.add(loadBalance);
                    // 记录已付款
                    final BigDecimal returnBalance = StatisticCoreEnterpriseEveryMonthPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    fourMonthReturnTotalBalance = fourMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 4) {
                    // 记录应付款
                    final BigDecimal loadBalance = StatisticCoreEnterpriseEveryMonthShouldPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    fiveMonthLoadTotalBalance = fiveMonthLoadTotalBalance.add(loadBalance);
                    // 记录已付款
                    final BigDecimal returnBalance = StatisticCoreEnterpriseEveryMonthPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    fiveMonthReturnTotalBalance = fiveMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 5) {
                    // 记录应付款
                    final BigDecimal loadBalance = StatisticCoreEnterpriseEveryMonthShouldPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    sixMonthLoadTotalBalance = sixMonthLoadTotalBalance.add(loadBalance);
                    // 记录已付款
                    final BigDecimal returnBalance = StatisticCoreEnterpriseEveryMonthPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    sixMonthReturnTotalBalance = sixMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 6) {
                    // 记录应付款
                    final BigDecimal loadBalance = StatisticCoreEnterpriseEveryMonthShouldPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    sevenMonthLoadTotalBalance = sevenMonthLoadTotalBalance.add(loadBalance);
                    // 记录已付款
                    final BigDecimal returnBalance = StatisticCoreEnterpriseEveryMonthPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    sevenMonthReturnTotalBalance = sevenMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 7) {
                    // 记录应付款
                    final BigDecimal loadBalance = StatisticCoreEnterpriseEveryMonthShouldPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    eightMonthLoadTotalBalance = eightMonthLoadTotalBalance.add(loadBalance);
                    // 记录已付款
                    final BigDecimal returnBalance = StatisticCoreEnterpriseEveryMonthPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    eightMonthReturnTotalBalance = eightMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 8) {
                    // 记录应付款
                    final BigDecimal loadBalance = StatisticCoreEnterpriseEveryMonthShouldPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    nineMonthLoadTotalBalance = nineMonthLoadTotalBalance.add(loadBalance);
                    // 记录已付款
                    final BigDecimal returnBalance = StatisticCoreEnterpriseEveryMonthPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    nineMonthReturnTotalBalance = nineMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 9) {
                    // 记录应付款
                    final BigDecimal loadBalance = StatisticCoreEnterpriseEveryMonthShouldPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    tenMonthLoadTotalBalance = tenMonthLoadTotalBalance.add(loadBalance);
                    // 记录已付款
                    final BigDecimal returnBalance = StatisticCoreEnterpriseEveryMonthPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    tenMonthReturnTotalBalance = tenMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 10) {
                    // 记录应付款
                    final BigDecimal loadBalance = StatisticCoreEnterpriseEveryMonthShouldPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    elevenMonthLoadTotalBalance = elevenMonthLoadTotalBalance.add(loadBalance);
                    // 记录已付款
                    final BigDecimal returnBalance = StatisticCoreEnterpriseEveryMonthPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    elevenMonthReturnTotalBalance = elevenMonthReturnTotalBalance.add(returnBalance);
                } else if (i == 11) {
                    // 记录应付款
                    final BigDecimal loadBalance = StatisticCoreEnterpriseEveryMonthShouldPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    twelveMonthLoadTotalBalance = twelveMonthLoadTotalBalance.add(loadBalance);
                    // 记录已付款
                    final BigDecimal returnBalance = StatisticCoreEnterpriseEveryMonthPayBalance(coreCustNo,
                            currentDate, currentMonthLastDay);
                    twelveMonthReturnTotalBalance = twelveMonthReturnTotalBalance.add(returnBalance);
                }
            }
        }
        // 记录应付款十二个月信息
        statisticLoadMap.put("11", twoMonthLoadTotalBalance);
        statisticLoadMap.put("10", threeMonthLoadTotalBalance);
        statisticLoadMap.put("9", fourMonthLoadTotalBalance);
        statisticLoadMap.put("8", fiveMonthLoadTotalBalance);
        statisticLoadMap.put("7", sixMonthLoadTotalBalance);
        statisticLoadMap.put("6", sevenMonthLoadTotalBalance);
        statisticLoadMap.put("5", eightMonthLoadTotalBalance);
        statisticLoadMap.put("4", nineMonthLoadTotalBalance);
        statisticLoadMap.put("3", tenMonthLoadTotalBalance);
        statisticLoadMap.put("2", elevenMonthLoadTotalBalance);
        statisticLoadMap.put("1", twelveMonthLoadTotalBalance);

        // 记录已付款十二个月信息
        statisticReturnMap.put("11", twoMonthReturnTotalBalance);
        statisticReturnMap.put("10", threeMonthReturnTotalBalance);
        statisticReturnMap.put("9", fourMonthReturnTotalBalance);
        statisticReturnMap.put("8", fiveMonthReturnTotalBalance);
        statisticReturnMap.put("7", sixMonthReturnTotalBalance);
        statisticReturnMap.put("6", sevenMonthReturnTotalBalance);
        statisticReturnMap.put("5", eightMonthReturnTotalBalance);
        statisticReturnMap.put("4", nineMonthReturnTotalBalance);
        statisticReturnMap.put("3", tenMonthReturnTotalBalance);
        statisticReturnMap.put("2", elevenMonthReturnTotalBalance);
        statisticReturnMap.put("1", twelveMonthReturnTotalBalance);
        // 记录应付款和已付款
        statisticPayAndLoadMap.put("payStatistic", statisticReturnMap);
        statisticPayAndLoadMap.put("loadStatistic", statisticLoadMap);
        return statisticPayAndLoadMap;

    }

    /**
     * 
     * @Title: StatisticEveryMonthBalance 
     * @Description: TODO(统计出资金方过去十二个月每月放款的金额) 
     * @param @param requestNo
     * @param @param currentDate
     * @param @param currentMonthLastDay
     * @param @return 参数说明 
     * @return BigDecimal 返回类型 
     * @throws 
     * @author chenlq
     * @date 2017年11月28日 上午11:48:28
     */

    BigDecimal StatisticEveryMonthLoadBalance(String factorNo, String currentDate, String currentMonthLastDay) {
        if (null != this.mapper.statisticEveryMonthLoadBalance(factorNo, currentDate, currentMonthLastDay)) {
            return this.mapper.statisticEveryMonthLoadBalance(factorNo, currentDate, currentMonthLastDay);
        } else {
            return new BigDecimal(0);
        }
    }

    /**
     * 
     * @Title: StatisticCoreEnterpriseEveryMonthLoadBalance 
     * @Description: TODO(统计核心企业过去应付款金额) 
     * @param @param requestNo
     * @param @param currentDate
     * @param @param currentMonthLastDay
     * @param @return 参数说明 
     * @return BigDecimal 返回类型 
     * @throws 
     * @author chenlq
     * @date 2017年11月29日 下午4:55:59
     */
    BigDecimal StatisticCoreEnterpriseEveryMonthShouldPayBalance(String coreCustNo, String currentDate,
            String currentMonthLastDay) {
        if (null != this.mapper.statisticcoreEnterpriseEveryMonthLoadBalance(coreCustNo, currentDate,
                currentMonthLastDay)) {
            return this.mapper.statisticcoreEnterpriseEveryMonthLoadBalance(coreCustNo, currentDate,
                    currentMonthLastDay);
        } else {
            return new BigDecimal(0);
        }
    }

    /**
     * 
     * @Title: StatisticEveryMonthpayBalance 
     * @Description: TODO(统计出核心企业过去每月已付款金额) 
     * @param @param requestNo
     * @param @param currentDate
     * @param @param currentMonthLastDay
     * @param @return 参数说明 
     * @return BigDecimal 返回类型 
     * @throws 
     * @author chenlq
     * @date 2017年11月28日 下午3:30:49
     */
    BigDecimal StatisticCoreEnterpriseEveryMonthPayBalance(String coreCustNo, String currentDate,
            String currentMonthLastDay) {
        if (null != this.mapper.statisticCoreEnterPriseEveryMonthPayBalance(coreCustNo, currentDate,
                currentMonthLastDay)) {
            return this.mapper.statisticCoreEnterPriseEveryMonthPayBalance(coreCustNo, currentDate,
                    currentMonthLastDay);
        } else {
            return new BigDecimal(0);
        }
    }

    /**
     * 
     * @Title: StatisticEveryMonthpayBalance 
     * @Description: TODO(统计出资金方过去十二个月没有回款金额) 
     * @param @param requestNo
     * @param @param currentDate
     * @param @param currentMonthLastDay
     * @param @return 参数说明 
     * @return BigDecimal 返回类型 
     * @throws 
     * @author chenlq
     * @date 2017年11月28日 下午3:30:49
     */
    BigDecimal StatisticEveryMonthReturnBalance(String factorNo, String currentDate, String currentMonthLastDay) {
        if (null != this.mapper.statisticEveryMonthPayBalance(factorNo, currentDate, currentMonthLastDay)) {
            return this.mapper.statisticEveryMonthPayBalance(factorNo, currentDate, currentMonthLastDay);
        } else {
            return new BigDecimal(0);
        }
    }

    /**
     * 
     * @Title: getLastDayOfMonth 
     * @Description: TODO(得到指定日期当月的最后一天) 
     * @param @param date
     * @param @return 参数说明 
     * @return String 返回类型 
     * @throws 
     * @author chenlq
     * @date 2017年11月28日 下午3:01:47
     */
    public String getLastDayOfMonth(String date) {
        final int year = Integer.parseInt(date.substring(0, 4));
        final int month = Integer.parseInt(date.substring(4, 6));
        final Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置月份
        cal.set(Calendar.MONTH, month - 1);
        // 获取某月最大天数
        final int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        // 设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        // 格式化日期
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        final String lastDayOfMonth = sdf.format(cal.getTime());
        return lastDayOfMonth;
    }

    /**
     * 
     * @Title: queryStatisticTotalBalanceIncome 
     * @Description: TODO(查询出各项费用) 
     * @param @return 参数说明 
     * @return Map<String,Object> 返回类型 
     * @throws 
     * @author chenlq
     * @date 2017年11月23日 下午1:32:46
     */
    public Map<String, Object> queryStatisticTotalBalanceIncome(Map<String, Object> anMap) {
        final Map<String, Object> totalBalance = new HashMap<>();
        final String factorNo = anMap.get("factorNo").toString();

        // 利息
        BigDecimal interestBalance = this.mapper.statisticTotalInterestPayBalance(factorNo);
        // 保理手术费
        BigDecimal OperationBalance = this.mapper.statisticTotalOperationPayBalance(factorNo);
        // 应收账款管理费
        BigDecimal managerBalance = this.mapper.statisticTotalManagerPayBalance(factorNo);

        // 其他费用
        BigDecimal otherBalance = this.mapper.statisticTotalOtherPayBalance(factorNo);

        // 合计
        BigDecimal accountTotalBalance = new BigDecimal(0);

        if (interestBalance != null) {
            accountTotalBalance = accountTotalBalance.add(interestBalance);
        } else {
            interestBalance = new BigDecimal(0);
        }
        if (OperationBalance != null) {
            accountTotalBalance = accountTotalBalance.add(OperationBalance);
        } else {
            OperationBalance = new BigDecimal(0);
        }
        if (managerBalance != null) {
            accountTotalBalance = accountTotalBalance.add(managerBalance);
        } else {
            managerBalance = new BigDecimal(0);
        }
        if (otherBalance != null) {
            accountTotalBalance = accountTotalBalance.add(otherBalance);
        } else {
            otherBalance = new BigDecimal(0);
        }

        totalBalance.put("interestBalance", interestBalance);
        totalBalance.put("OperationBalance", OperationBalance);
        totalBalance.put("managerBalance", managerBalance);
        totalBalance.put("otherBalance", otherBalance);
        totalBalance.put("accountTotalBalance", accountTotalBalance);

        return totalBalance;

    }

}
